<?php
/**
    c2earn 基类库
	@author xiaoming <maixiaoming@9qi.cn>
*/
class BaseAction extends Action 
{
	protected $userName;
	protected $userId;
	protected $userPoints;
	
	/**
		系统初始化函数
	*/
	function _initialize()
	{
		//$this->_checkLogin();
		$this->_getPoints();
		
		//生成导航地址
		$this->assign('nav_index', U("Apps://index/index"));
		$this->assign('nav_coupons', U("Apps://coupon/index"));
		$this->assign('nav_login', U("Apps://index/login")); //登錄
		$this->assign('nav_register', U("Apps://index/register"));//註冊
		$this->assign('nav_logout', U("Apps://index/logout"));//退出

		$this->userName = Session::get('u_name');
		$this->userId = Session::get('u_id');
		$this->assign('userName',$this->userName);

		$this->assign('MODULE_NAME',MODULE_NAME);
		
		$this->assign('title','hello,c2earn!');
	}
	
	/**
		检查会员登录情况
		@access protected
		@return void
	*/
	protected function _checkLogin()
	{
		if (!( Session::is_set('u_id') && (Session::get('u_id') > 0)) ){
			$this->redirect('index/login', array(), 3, 'please login');
			exit();
		} else {
			$this->userName = Session::get('u_name');
			$this->userId = Session::get('u_id');
			$this->assign('userName',$this->userName);
			$this->assign('userId',$this->userId);
		}
	}
	
	/**
		显示消息页面并跳转
		@access protected
		@param string $title     页面标题
			   string $jumpUrl   跳转URL
			   string $message   页面提示信息
			   int    $waitSecond   等待时间 
	    @return void
	*/
	protected function _message($title,$jumpUrl,$message,$waitSecond=5)
	{
		$this->assign("jumpUrl",$jumpUrl);
		$this->assign('waitSecond',$waitSecond);
		$this->assign('title',$title);
		$this->success($message);
	}
	
	/**
		获取当前会员积分
		@access protected

		@return int $points 积分值
	*/
	protected function _getPoints()
	{
		$u_id = Session::get('u_id');
		$User = M('user');
		$this->userPoints = $User->where("u_id=$u_id")->getField('u_points');
		$this->assign('userPoints',$this->userPoints);
	}
	
	/**
		检查该会员今天是否已经看过此视频
		@access protected
		
		@param  int $u_id  会员ID
				int $video_id 广告视频ID
		
		@return bool    true--已经看过此视频
						false-还没观看
		
	*/
	protected function _checkViewed($u_id, $video_id) {
		$Log = M('log');
		$start = date('Y-m-d',time());  //一天开始时间
		$end = date('Y-m-d 23:59:59',time());   //一天结束时间
		$start = strtotime($start);
		$end = strtotime($end);
		
		$result = $Log->where("u_id=$u_id AND video_id=$video_id AND view_time > $start AND view_time < $end")->find();
		if($result) {
			return true;
		} else {
			return false;
		}
	}
	

	
	/**
		页面无法找到(404)时的处理函数
	*/
	protected function _pageNotFound()
	{
		$this->redirect('index/index', array(), 3, 'Page Not Found!');
	}
}